import os
from PyQt5.QtCore import Qt, QModelIndex, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QPushButton,\
                            QCheckBox,\
                            QComboBox,\
                            QDataWidgetMapper,\
                            QDialog,\
                            QDialogButtonBox,\
                            QGridLayout,\
                            QLabel,\
                            QLineEdit,\
                            QMessageBox
from PyQt5.QtSql import QSqlTableModel


class EditDialog(QDialog):
    edited = pyqtSignal(str, str, str, str, str)

    def __init__(self, parent, rbkmodel, rbksmodel):
        super().__init__(parent)
        self.setWindowTitle('Edit Rulebook Properties...')
        self.parent = parent
        self.rbkmodel = rbkmodel
        self.rbksmodel = rbksmodel
        # A separate model for the Rulebooks table, one we can filter to just core books.
        self.rbksCoreModel = QSqlTableModel(None, rbksmodel.database())
        self.rbksCoreModel.setTable(rbksmodel.tableName())

        self.rbkmodel.setFilter('')
        self.rbkmodel.select()
        mapper = QDataWidgetMapper()
        mapper.setModel(self.rbkmodel)
        
        self.rbksmodel.setFilter('')
        self.rbksmodel.setSort(self.rbkmodel.fieldIndex('GameName'), Qt.AscendingOrder)
        self.rbksmodel.select()

        self.rbksCoreModel.setFilter('')
        self.rbksCoreModel.setSort(self.rbkmodel.fieldIndex('GameName'), Qt.AscendingOrder)
        self.rbksCoreModel.select()

        grid = QGridLayout()
        self.resize(500, 200)

        gamelabel = QLabel('Select Game...')
        grid.addWidget(gamelabel, 0, 0, 1, 1)
        
        self.game = QComboBox()
        self.gamelist = []
        self.gamelist.append('New Game...')
        for i in range(self.rbksmodel.rowCount()):
            self.gamelist.append(self.rbksmodel.record(i).value('GameName'))
        self.game.addItems(self.gamelist)
        self.game.currentIndexChanged.connect(self.gameselected)
        mapper.addMapping(self.game, 0)
        grid.addWidget(self.game, 0, 1, 1, 2)

        self.gamenew = QLineEdit()
        grid.addWidget(self.gamenew, 0, 3, 1, 2)

        versionlabel = QLabel('Select Version...')
        grid.addWidget(versionlabel, 1, 0, 1, 1)

        self.version = QComboBox()
        mapper.addMapping(self.version, 1)
        grid.addWidget(self.version, 1, 1, 1, 2)

        self.versionnew = QLineEdit()
        grid.addWidget(self.versionnew, 1, 3, 1, 2)
        
        rbklabel = QLabel('New Rulebook Name...')
        grid.addWidget(rbklabel, 2, 0, 1, 1)

        self.rbkname = QLineEdit()
        mapper.addMapping(self.rbkname, 2)
        grid.addWidget(self.rbkname, 2, 1, 1, 4)

        self.core = QCheckBox('Core', self)
        self.core.toggle()
        self.core.stateChanged.connect(self.coreToggle)
        mapper.addMapping(self.core, 3)
        grid.addWidget(self.core, 3, 0, 1, 1)

        self.corerbk = QComboBox()
        mapper.addMapping(self.corerbk, 4)
        self.corerbk.hide()
        grid.addWidget(self.corerbk, 3, 1, 1, 4)

        self.buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        #self.buttons.accepted.connect(self.onOk)
        self.buttons.rejected.connect(self.onCancel)
        grid.addWidget(self.buttons, 4, 0, 1, 4)

        mapper.toFirst()
        self.setLayout(grid)


    @pyqtSlot()
    def gameselected(self):
        self.version.clear()
        self.corerbk.clear()
        gamename = self.game.currentText()

        if(gamename == 'New Game...'):
            self.gamenew.show()
            self.versionnew.show()
        else:
            self.gamenew.hide()
            self.versionnew.hide()
            filter = 'GameName = "{0}"'.format(gamename)
            filter2 = 'Core = {0}'.format(Qt.Checked)
            
            self.rbkmodel.setFilter(filter)
            self.rbkmodel.setSort(self.rbkmodel.fieldIndex('GameVersion'), Qt.DescendingOrder)
            self.rbkmodel.select()
            versions = []
            for i in range(self.rbkmodel.rowCount()):
                versions.append(str(self.rbkmodel.record(i).value('GameVersion')))
            self.version.addItems(versions)

            self.rbksCoreModel.setFilter(', '.join((filter, filter2)))
            self.rbksCoreModel.setSort(self.rbksCoreModel.fieldIndex('GameVersion'), Qt.DescendingOrder)
            self.rbksCoreModel.select()
            coreversions = []
            for i in range(self.rbksCoreModel.rowCount()):
                coreversions.append(str(self.rbksCoreModel.record(i).value('GameVersion')))
            self.corerbk.addItems(coreversions)

    def coreToggle(self, state):
        if state == Qt.Checked:
            self.corerbk.hide()
        else:
            self.corerbk.show()


    @pyqtSlot()
    def onCancel(self):
        self.close()
